package com.ncst.backend.controller;

import com.ncst.backend.common.Result;
import com.ncst.backend.domain.DrugInfo;
import com.ncst.backend.service.DrugInfoService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 药品信息控制器
 */
@RestController
@RequestMapping("/drug-infos")
public class DrugInfoController {

    @Autowired
    private DrugInfoService drugInfoService;

    /**
     * 根据ID查询药品信息
     */
    @ApiOperation(value = "根据ID查询药品信息")
    @GetMapping("/{id}")
    public Result<DrugInfo> getById(@PathVariable Integer id) {
        DrugInfo drugInfo = drugInfoService.getById(id);
        if (drugInfo == null) {
            return Result.notFound("药品不存在");
        }
        return Result.success(drugInfo);
    }

    /**
     * 查询所有药品信息
     */
    @ApiOperation(value = "查询所有药品信息")
    @GetMapping("/all")
    public Result<List<DrugInfo>> getAll() {
        List<DrugInfo> list = drugInfoService.getAll();
        return Result.success(list);
    }

    /**
     * 根据条件查询药品信息
     */
    @ApiOperation(value = "根据条件查询药品信息")
    @PostMapping("/condition")
    public Result<List<DrugInfo>> getByCondition(@RequestBody DrugInfo drugInfo) {
        List<DrugInfo> list = drugInfoService.getByCondition(drugInfo);
        return Result.success(list);
    }

    /**
     * 分页查询药品信息
     */
    @ApiOperation(value = "分页查询药品信息")
    @GetMapping("/page")
    public Result<List<DrugInfo>> getByPage(@RequestParam(defaultValue = "1") Integer pageNum,
                                          @RequestParam(defaultValue = "10") Integer pageSize) {
        List<DrugInfo> list = drugInfoService.getByPage(pageNum, pageSize);
        return Result.success(list);
    }

    /**
     * 查询药品总数
     */
    @ApiOperation(value = "查询药品总数")
    @GetMapping("/count")
    public Result<Long> countAll() {
        Long count = drugInfoService.countAll();
        return Result.success(count);
    }

    /**
     * 根据条件查询药品总数
     */
    @ApiOperation(value = "根据条件查询药品总数")
    @PostMapping("/count/condition")
    public Result<Long> countByCondition(@RequestBody DrugInfo drugInfo) {
        Long count = drugInfoService.countByCondition(drugInfo);
        return Result.success(count);
    }

    /**
     * 新增药品信息
     */
    @ApiOperation(value = "新增药品信息")
    @PostMapping
    public Result<Boolean> save(@RequestBody DrugInfo drugInfo) {
        boolean success = drugInfoService.save(drugInfo);
        if (success) {
            return Result.success("新增成功", true);
        }
        return Result.error("新增失败");
    }

    /**
     * 批量新增药品信息
     */
    @ApiOperation(value = "批量新增药品信息")
    @PostMapping("/batch")
    public Result<Boolean> saveBatch(@RequestBody List<DrugInfo> list) {
        boolean success = drugInfoService.saveBatch(list);
        if (success) {
            return Result.success("批量新增成功", true);
        }
        return Result.error("批量新增失败");
    }

    /**
     * 更新药品信息
     */
    @ApiOperation(value = "更新药品信息")
    @PutMapping
    public Result<Boolean> updateById(@RequestBody DrugInfo drugInfo) {
        if (drugInfo.getId() == null) {
            return Result.error("ID不能为空");
        }
        boolean success = drugInfoService.updateById(drugInfo);
        if (success) {
            return Result.success("更新成功", true);
        }
        return Result.error("更新失败");
    }

    /**
     * 根据ID删除药品信息
     */
    @ApiOperation(value = "根据ID删除药品信息")
    @DeleteMapping("/{id}")
    public Result<Boolean> removeById(@PathVariable Integer id) {
        boolean success = drugInfoService.removeById(id);
        if (success) {
            return Result.success("删除成功", true);
        }
        return Result.error("删除失败");
    }

    /**
     * 批量删除药品信息
     */
    @ApiOperation(value = "批量删除药品信息")
    @DeleteMapping("/batch")
    public Result<Boolean> removeByIds(@RequestBody List<Integer> ids) {
        boolean success = drugInfoService.removeByIds(ids);
        if (success) {
            return Result.success("批量删除成功", true);
        }
        return Result.error("批量删除失败");
    }
}
